package com.project.common.utils.file;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class FileUploadUtil {
    //文件根目录
    private static final String FILE_PATH = "C://";

    public ResultCommon upload(MultipartFile[] multipartFile)throws IOException {
        if(multipartFile==null)return new ResultCommon(7001,"文件上传失败");
        for (MultipartFile file : multipartFile) {
            //eg：a.jpg
            String filename = file.getOriginalFilename();
            String substring = filename.substring(filename.lastIndexOf("."));
            String filePrefix = UUID.randomUUID().toString();
            String filePath=FILE_PATH+filePrefix+substring;
            file.transferTo(new File(filePath));
        }
        return new ResultCommon(200,"文件上传成功");
    }


}
//学习链接：https://blog.csdn.net/weixin_49161209/article/details/108331947?spm=1001.2101.3001.6650.7&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-7-108331947-blog-102581759.235%5Ev27%5Epc_relevant_3mothn_strategy_and_data_recovery&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-7-108331947-blog-102581759.235%5Ev27%5Epc_relevant_3mothn_strategy_and_data_recovery&utm_relevant_index=8